package com.zzy.controller;

import com.zzy.domain.dto.PageResultDto;
import com.zzy.domain.Result;
import com.zzy.domain.User;
import com.zzy.domain.vo.PageQuery;
import com.zzy.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RequiredArgsConstructor
@RestController
@RequestMapping("/user")
public class UserController {
    private final UserService userService;

    @GetMapping("/page")
    public Result page(PageQuery pageQuery){
        PageResultDto<User> pageResultDto=userService.page(pageQuery);
        return Result.success(pageResultDto);
    }

    @PostMapping("/add")
    public Result add(@RequestBody User user){
        userService.add(user);
        return Result.success();
    }

    /**
     * 回显功能修正：返回查询到的用户数据
     */
    @GetMapping("/show/{id}")
    public Result showid(@PathVariable Integer id){
        User user = userService.show(id);
        return Result.success(user);
    }

    /**
     * 修改功能修正：接收用户数据并完成更新
     */
    @PutMapping("/update")
    public Result update(@RequestBody User user){
        userService.update(user);
        return Result.success();
    }

    /**
     * 新增删除功能
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        userService.delete(id);
        return Result.success();
    }

}